package org.example.forum.services.impl;

import lombok.extern.slf4j.Slf4j;
import org.example.forum.common.AppResult;
import org.example.forum.common.ResultCode;
import org.example.forum.dao.BoardMapper;
import org.example.forum.exception.ApplicationException;
import org.example.forum.model.Board;
import org.example.forum.services.IBoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
@Slf4j
public class BoradServiceImpl implements IBoardService {
    @Autowired
    private BoardMapper boardMapper;
    @Override
    public List<Board> selectByNum(Integer num) {
        if(num<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //调用dao查询数据
        List<Board> boards = boardMapper.selectByNum(num);
        return boards;
    }

    @Override
    public void addOneArticleCountById(Long id) {

        if(id==null||id<=0){
            //打印异常
            log.warn(ResultCode.FAILD_BOARD_ARTICLE_COUNT.toString());
            //抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILD_BOARD_ARTICLE_COUNT));
        }
        //查询对应的板块
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board==null){
            //打印日志
            log.warn(ResultCode.ERROR_IS_NULL.toString());
            //抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_IS_NULL));
        }
        //更新帖子数量
        Board updateBoard=new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount()+1);
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(row!=1){
            log.warn(ResultCode.FAILED.toString()+"受影响的行数大于1");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }


    }

    @Override
    public Board selectById(Long id) {
        if(id==null||id<=0){
            //打印异常
            log.warn(ResultCode.FAILD_BOARD_ARTICLE_COUNT.toString());
            //抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILD_BOARD_ARTICLE_COUNT));
        }
        Board board = boardMapper.selectByPrimaryKey(id);
        return board;
    }

    @Override
    public void subOneArticleCountById(Long id) {
        if(id==null||id<=0){
            //打印异常
            log.warn(ResultCode.FAILD_BOARD_ARTICLE_COUNT.toString());
            //抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILD_BOARD_ARTICLE_COUNT));
        }

        //查询当前板块的帖子数
        Board board = boardMapper.selectByPrimaryKey(id);
        if(board==null){
            //打印异常
            log.warn(ResultCode.FAILED_BOARD_NOT_EXISTS.toString());
            //抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_NOT_EXISTS));
        }

        Board updateBoard=new Board();
        updateBoard.setId(board.getId());
        updateBoard.setArticleCount(board.getArticleCount()-1);
        //判断在减一后数字为负数的情况
        if(updateBoard.getArticleCount()<0){
            updateBoard.setArticleCount(0);
        }
        //调用dao
        int row = boardMapper.updateByPrimaryKeySelective(updateBoard);
        if(row!=1){
            log.warn(ResultCode.FAILED.toString()+"受影响的行数大于1");
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }
    }



}
